package com.haochen.generation.controller;

import com.haochen.generation.domain.AjaxResult;
import com.haochen.generation.domain.GenTable;
import com.haochen.generation.service.GenTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: zhangquancheng
 * @Description: 代码生成
 * @Date: 2023/3/30 4:22 下午
 */
@RestController
public class GenController extends BaseController {

    @Autowired
    private GenTableService genTableService;

    /**
     * 导入表结构（保存）
     */
    @PostMapping("/importTable")
    @ResponseBody
    public AjaxResult importTableSave(String tables) {
        String[] tableNames = tables.split(",");
        // 查询表信息
        List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
        String operName = "System";
        genTableService.importGenTable(tableList, operName);
        return AjaxResult.success();
    }

    /**
     * 同步数据库
     */
    @GetMapping("/synchDb/{tableName}")
    @ResponseBody
    public AjaxResult synchDb(@PathVariable("tableName") String tableName) {
        genTableService.synchDb(tableName);
        return AjaxResult.success();
    }

    /**
     * 生成代码（自定义路径）
     */
    @GetMapping("/genCode/{tableName}")
    public AjaxResult genCode(@PathVariable("tableName") String tableName) throws IOException {
        genTableService.generatorCode(tableName);
        return success();
    }

}
